package com.cskaoyan.market.controller;

import com.cskaoyan.market.bo.GoodsCombineBo;
import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.service.AdminGoodsService;
import com.cskaoyan.market.service.AdminGoodsServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.core.JsonProcessingException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/goods/*")
public class AdminGoodsController extends HttpServlet {
    AdminGoodsService adminGoodsService=new AdminGoodsServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI=req.getRequestURI();
        String op=requestURI.replace(req.getContextPath()+"/admin/goods/","");
        if("list".equals(op)){
            list(req,resp);
        }
        if("catAndBrand".equals(op)){
            catAndBrand(req,resp);
        }
    }
    private void catAndBrand(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String,Object> data=adminGoodsService.catAndBrand();
        Object ok =ResponseUtil.ok(data);
        String s =JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().write(s);
    }
    private void list(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String page = req.getParameter("page");
            String limit = req.getParameter("limit");
            String sort = req.getParameter("sort");
            String order = req.getParameter("order");
            String goodsSn=req.getParameter("goodsSn");
            String name=req.getParameter("name");
            String goodsId=req.getParameter("goodsId");
            List<MarketGoods> goodsList=adminGoodsService.list(page,limit,sort,order,goodsSn,name,goodsId);
            Object ok = ResponseUtil.okList(goodsList);
            String s= JacksonUtil.getObjectMapper().writeValueAsString(ok);
            resp.getWriter().write(s);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI=req.getRequestURI();
        String op=requestURI.replace(req.getContextPath()+"/admin/goods/","");
        if("create".equals(op)){
            create(req,resp);
        }
    }

    private void create(HttpServletRequest req, HttpServletResponse resp) {
        try {
            BufferedReader reader = req.getReader();
            String requestBody = reader.readLine();
            GoodsCombineBo goodsCombineBo = JacksonUtil.getObjectMapper().readValue(requestBody, GoodsCombineBo.class);
            adminGoodsService.insert(goodsCombineBo.getGoods(), goodsCombineBo.getSpecifications(), goodsCombineBo.getAttributes(), goodsCombineBo.getProducts());

            //抓取公网上面的返回结果
            Object ok = ResponseUtil.ok();
            String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
            resp.getWriter().println(s);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
